package heliotrope.res
{
	import heliotrope.global.ErrorHandler;
	import heliotrope.net.loader.ITargetHost;
	import heliotrope.net.loader.LoadState;
	import heliotrope.game.system.LoadSystem;
	import heliotrope.res.manager.GraphicLoadManager;
	import heliotrope.display.AutoBitmap;

	public class TileResource implements ITargetHost
	{
		
		private var _tileMatrix:Vector.<Vector.<AutoBitmap>>;
		private var _thumbMap:BitmapdataDistributor;
		private var _id:uint;
		private var _loadManager:GraphicLoadManager;
		public function TileResource(id_:uint, numX:uint, numY:uint)
		{
			_id = id_;
			_tileMatrix = new Vector.<Vector.<AutoBitmap>>(numX, true);
			_loadManager = GraphicLoadManager.getInstance();
			_thumbMap = new BitmapdataDistributor;
			for (var i:int = 0; i<numX; i++) 
			{
				_tileMatrix[i] = new Vector.<AutoBitmap>(numY, true);
				for(var j:int = 0; j<numY; j++)
				{
					_tileMatrix[i][j] = new AutoBitmap;
				}
			}
		}
		public function getThumb():BitmapdataDistributor
		{
			if(_thumbMap.loadState == LoadState.NOT_LOAD)
			{
				_thumbMap.loadState = LoadState.PRE_LOAD;
				_loadManager.loadThumb(_id, this, _thumbMap);
			}
			return _thumbMap;
		}
		public function getTile(x:uint, y:uint):AutoBitmap
		{
			var tile:AutoBitmap = _tileMatrix[x][y];
			if(tile.loadState == LoadState.NOT_LOAD)
			{
				tile.loadState = LoadState.PRE_LOAD;
				_loadManager.loadTile(_id, x, y, this, tile);
			}
			return _tileMatrix[x][y];
		}

		public function setLoadState(state_:LoadState):void
		{
			state_.target.loadState = state_.state;
		}
	}
}